import { Base64 } from 'js-base64'

const AA = '=Z'

export default class Crypt{
  static encrypt(word, key = 'C') {
    return Base64.encode(key + word)
  }
  static decrypt(word, key = 'C') {
    const str = Base64.decode(word)
    return str.substring(key.length)
  }

  static e(word, key = 'Y') {
    const m1 = Crypt.encrypt(word)
    const m2 = Crypt.encrypt(m1, key)
    return AA + m2
  }
  static d(word, key = 'Y') {
    const str = word.substring(AA.length)
    const m2 = Crypt.decrypt(str, key)
    const m1 = Crypt.decrypt(m2)
    return m1
  }

  static ee(word) {
    return Crypt.e(JSON.stringify(word))
  }
  static dd(word) {
    return JSON.parse(Crypt.d(word))
  }

  static encode(data) {
    if (data === undefined) {
      return undefined
    }
    if (data === null) {
      return null
    }
    return Crypt.ee(data)
  }
  static parse(data) {
    if (data === undefined) {
      return undefined
    }
    if (data === null) {
      return null
    }
    return Crypt.dd(data)
  }
}
